package ar.edu.itba.twiteros.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import ar.edu.itba.twiteros.domain.UserIds;
import ar.edu.itba.twiteros.util.Facebook;
import ar.edu.itba.twiteros.util.GrantTwitterAccess;

@SuppressWarnings("serial")
public class DirectServlet extends HttpServlet{

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		RequestDispatcher dispacher= getServletContext().getRequestDispatcher("/direct.jsp");

		Long uid= Long.valueOf(req.getParameter("fb_sig_user"));
		Twitter twitter = new Twitter();
		GrantTwitterAccess grantAccess= new GrantTwitterAccess(twitter, uid);
		
		if(grantAccess.setAccessToken())
		{
			req.setAttribute("access", true);
			try {
				req.setAttribute("direct_messages", twitter.getDirectMessages());
			} catch (TwitterException e) {
				req.setAttribute("error_message", e.getMessage());
				dispacher= getServletContext().getRequestDispatcher("/error.jsp");
			}
			
			
		}else{
			req.setAttribute("access", false);
		}
			
		dispacher.forward(req, resp);
	}


}

